/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.depaul.se.courseregistration.mdb.p2p;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author lmarco
 */
public class Sender {
    
    public static final String CONFIRMATION = "CONFIRMATION";
    public static final String ERROR = "ERROR";
    
    public Sender(){};
    public void sendMessage(String confirmation, int result) throws Exception{
        
        String conf = null;
        if(result==0)
            conf = Sender.CONFIRMATION;
        else
            conf = Sender.ERROR;
        
        // Get JNDI context
        Context jndiContext = new InitialContext();
        
        // Looks up the administered objects
        ConnectionFactory connectionFactory = (ConnectionFactory) 
                    jndiContext.lookup("jms/javaee6/ConnectionFactory");
        Queue queue = (Queue) jndiContext.lookup("jms/javaee6/Queue");
        
        // Connect to queue to send message
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        
        // Send message
        TextMessage message = session.createTextMessage();
        message.setText("\n-------" +conf +"-------" + "\n" + confirmation);
        System.out.println("\n-------" +conf +"-------" + "\n" + confirmation);
        producer.send(message);
        System.out.println("Message was sent");
        
        // Clean up
        connection.close();
    }
    
}
